package org.streets.eis.component.messagebox;

import org.apache.wicket.Component;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.model.Model;

import org.streets.eis.component.WebResources;
import org.streets.eis.component.messagebox.MessageBox.ButtonType;
import org.streets.eis.component.messagebox.MessageBox.DialogCallback;
import org.streets.eis.component.messagebox.MessageBox.IconType;
import org.streets.eis.component.messagebox.MessageBox.ReturnType;

public class BoxInnerPage extends WebPage implements IHeaderContributor{
	
	private static final long serialVersionUID = 1L;
	
	private MessageBox box;
	
	private Component defaultButton;
	
	public BoxInnerPage(final MessageBox box,String content,IconType iconType,ButtonType buttonType,final DialogCallback callback){
		super();
		this.box = box;
		
		add(new Label("messageText",content.replaceAll("\r\n|\r|\n", "<br/>")).setEscapeModelStrings(false));
		
		switch(iconType){
			case INFO :
				add(new Image("typeImg",new ResourceReference(WebResources.class, "res/icon-info.gif")));
				break;
			case QUESTION :
				add(new Image("typeImg",new ResourceReference(WebResources.class, "res/icon-question.gif")));
				break;				
			case WARNING:
				add(new Image("typeImg",new ResourceReference(WebResources.class, "res/icon-warning.gif")));
				break;				
			case ERROR:
				add(new Image("typeImg",new ResourceReference(WebResources.class, "res/icon-error.gif")));
				break;				
		}
		
		add(newButtonFragment("buttons",buttonType).setRenderBodyOnly(true));	
	}
	
	private final class AjaxButton extends AjaxLink<ReturnType>{
		private static final long serialVersionUID = 1L;
		
		public AjaxButton(String id,ReturnType returnType) {
			super(id,Model.of(returnType));
		}
			
		@Override
		protected void onComponentTag(ComponentTag tag){
			super.onComponentTag(tag);
			tag.put("style", "width:78px;height:23px;border:#ccc 1px solid;");
		}
		@Override
		public void onClick(AjaxRequestTarget target) {
			box.returnType = (ReturnType)getDefaultModelObject();
			box.modalWindow.close(target);				
		}			
	}
	
	private Fragment newButtonFragment(String id,ButtonType buttonType){
		switch (buttonType) {
		case YESNO:
			return new Fragment(id, "YESNO", this) {
				private static final long serialVersionUID = 1L;
				{
					add(new AjaxButton("yes", ReturnType.YES));
					add(defaultButton = new AjaxButton("no", ReturnType.NO));
				}
			};
		case OKCANCEL:
			return new Fragment(id, "OKCANCEL", this) {
				private static final long serialVersionUID = 1L;

				{
					add(new AjaxButton("ok", ReturnType.OK));
					add(defaultButton = new AjaxButton("cancel", ReturnType.CANCEL));
				}
			};
		default:
			return new Fragment(id, "OK", this) {
				private static final long serialVersionUID = 1L;

				{
					add(defaultButton = new AjaxButton("ok", ReturnType.OK));
				}
			};
		}  		 
	}

	public void renderHead(IHeaderResponse response) {
		if (defaultButton != null){
	          String js = String.format("var f = document.getElementById('%s'); if (f){f.focus();}", defaultButton.getMarkupId()); 
	          response.renderOnDomReadyJavascript(js); 			
        }
	}

}
